package com.example.mvpdemo.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mvpdemo.util.ActivityManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author:created by njb
 * 时间:2019/3/8 15 23
 * 描述:
 */
public abstract class BaseActivity <P extends BasePresenter> extends AppCompatActivity implements BaseView {
    public Context context;
    public Toast toast;
    protected P presenter;
    protected Unbinder unbinder;

    protected abstract P createPresenter();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void addListener();

    private ProgressDialog dialog;
    private View view;
    private ImageView iv_loading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityManager.getInstance().pushOneActivity(this);
        //setWhiteStatusBar();
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        unbinder = ButterKnife.bind(this);
        context = this;
        presenter = createPresenter();
        initView();
        addListener();

    }

/*
    */
/**
     * 白色状态栏
     *//*

    protected void setWhiteStatusBar() {
        //设置状态栏颜色;
        StatusBarUtil.setColorForSwipeBack(this,
                getResources().getColor(R.color.cffffff), 0);
        //暗色
        StatusBarUtils.StatusBarLightMode(this, StatusBarUtils.StatusBarLightMode(this));
    }

    */
/**
     * 黑色状态栏
     *//*

    public void setBlackStausBar() {
        StatusBarUtil.setColorForSwipeBack(this,
                getResources().getColor(R.color.c151520), 0);
        //亮色
        StatusBarUtils.StatusBarDarkMode(this, StatusBarUtils.StatusBarLightMode(this));
    }

    */
/**
     * 设置 无状态栏 亮色模式（状态栏黑色字体）
     *//*

    public void setNoStatusBarByLight() {
        StatusBarUtil.setTranslucentForImageView(this, 0, null);
        StatusBarUtils.StatusBarLightMode(this, StatusBarUtils.StatusBarLightMode(this));
    }

    */
/**
     * 设置 无状态栏 暗色模式（状态栏白色字体）
     *//*

    public void setNoStatusBarByDrak() {
        StatusBarUtil.setTranslucentForImageView(this, 0, null);
        //亮色
        StatusBarUtils.StatusBarDarkMode(this, StatusBarUtils.StatusBarLightMode(this));
    }
*/


    /**
     * 打开Activity
     *
     * @param cls
     */
    public void startA(@NonNull Class<?> cls) {
        if (cls == null) {
            return;
        }

        Intent intent = new Intent(context, cls);
        startActivity(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().popOneActivity(this);
        if (presenter != null) {
            presenter.detachView();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }

    }

    /**
     * @param s
     */
    public void showtoast(@NonNull String s) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }


/*    *//**
     * 显示加载动画
     *//*
    public void showLoadingDialog() {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        if (view == null) {
            view = getViewByRes(R.layout.dialog_loading);
        }
        if (iv_loading == null) {
            iv_loading = view.findViewById(R.id.iv_loading);
        }
        RequestOptions options = new RequestOptions()
                .centerInside()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);

        Glide.with(context).load(R.mipmap.loading).apply(options).into(iv_loading);
        if (dialog == null) {
            dialog = new ProgressDialog(context, R.style.dialog);
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(view);
    }*/

    /**
     * 隐藏加载动画
     */
    public void closeLoadingDialog() {
        if (dialog != null && dialog.isShowing()) {
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                }
            }, 300);
        }
    }


    public void showFileDialog() {
    }

    public void hideFileDialog() {
    }


    /**
     * 通过资源res获得view
     *
     * @param res
     * @return
     */
    public View getViewByRes(@LayoutRes int res) {
        return LayoutInflater.from(context).inflate(res, null);
    }

    /**
     * 获得TextView 的文本
     *
     * @param tv
     * @return
     */
    public String getTV(TextView tv) {
        return tv == null ? "" : tv.getText().toString().trim();
    }


    @Override
    public void showLoading() {
        //howLoadingDialog();
    }

    @Override
    public void hideLoading() {
        closeLoadingDialog();
    }

    @Override
    public void showError(String msg) {
        showtoast(msg);
    }

    @Override
    public void onErrorCode(Model model) {
        if (model != null) {
        }
    }

    @Override
    public void showLoadingFileDialog() {
        showFileDialog();
    }

    @Override
    public void hideLoadingFileDialog() {
        hideFileDialog();
    }

    @Override
    public void onProgress(long totalSize, long downSize) {
    }

}
